package top.biangege.bbs.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.biangege.bbs.aop.annotation.RedisCache;
import top.biangege.bbs.entity.Video;
import top.biangege.bbs.entity.VideoItem;
import top.biangege.bbs.mapper.VideoItemMapper;
import top.biangege.bbs.mapper.VideoMapper;
import top.biangege.bbs.service.VideoItemService;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

@Service
public class VideoItemServiceImpl extends ServiceImpl<VideoItemMapper, VideoItem> implements VideoItemService {

    @Resource
    private VideoItemMapper videoItemMapper;

    @Resource
    private VideoMapper videoMapper;

    @Override
//    @RedisCache
    @Transactional(rollbackFor = Exception.class)
    public void test() {
        System.out.println("开始执行");
        //PageHelper.startPage(1, 10);
        List<Video> videos = videoMapper.selectList(new QueryWrapper<Video>().last("limit 4454,100"));
        System.out.println(videos.size());
        //PageInfo<Video> videoPageInfo = new PageInfo<>(videos);

        for (int i = 0; i < videos.size(); i++) {
            System.out.println("第" + i + "条数据开始");
            String s = doGet("https://taopianapi.com/home/cjapi/as/mc10/vod/json?ac=videolist&t=&pg=&h=&ids=" + videos.get(i).getVod_id() + "&wd=");
            JSONObject jsonObject = JSONObject.parseObject(s);
            List<VideoItem> list = jsonObject.getJSONArray("list").toJavaList(VideoItem.class);
            int insert = videoItemMapper.insert(list.get(0));
            System.out.println("第" + i + "条数据结束-----影响行数: " + insert);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        //doGet("https://taopianapi.com/home/cjapi/as/mc10/vod/json?ac=videolist&t=&pg=&h=&ids=43589&wd=");


        //System.out.println(videoPageInfo.getPages());
    }


    public static String doGet(String httpUrl) {
        //链接
        HttpURLConnection connection = null;
        InputStream is = null;
        BufferedReader br = null;
        StringBuffer result = new StringBuffer();
        try {
            //创建连接
            java.net.URL url = new URL(httpUrl);
            connection = (HttpURLConnection) url.openConnection();
            //设置请求方式
            connection.setRequestMethod("GET");
            //设置连接超时时间
            connection.setConnectTimeout(15000);
            //设置读取超时时间
            connection.setReadTimeout(15000);
            //开始连接
            connection.connect();
            //获取响应数据
            if (connection.getResponseCode() == 200) {
                //获取返回的数据
                is = connection.getInputStream();
                if (is != null) {
                    br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                    String temp = null;
                    while ((temp = br.readLine()) != null) {
                        result.append(temp);
                    }
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            connection.disconnect();// 关闭远程连接
        }
        return result.toString();
    }
}
